import React, { useState } from 'react';
import { message } from 'antd';
import ScoreModal from './index';
import { applyReScore, checkTaskBeforeApply, getReRateEmployeeList, getOperateDetails } from '../../services';
import { modalTypes } from './modalTypes';

const ReRateApplyModal = ({ ReScoreModalRef, setActionLoading, isDetail = false }) => {
    const [modalData, setModalData] = useState();
    const [modalVisible, setModalVisible] = useState(false);
    const [modalType, setModalType] = useState('');
    const user = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {};
    const showReScoreModal = async record => {
        let reRateEmployeeList = [];
        setActionLoading(true);
        const res = await getReRateEmployeeList({ taskId: record.id });
        if (res.code === 10000) {
            if (res.data.length > 0) {
                reRateEmployeeList = res.data.map(item => ({
                    label: item.employeeName,
                    value: JSON.stringify(item)
                }));
            }
        }
        if (reRateEmployeeList.length === 0) {
            setActionLoading(false);
            return message.warning('该任务暂无可重评人员');
        }
        const { data = {} } = await checkTaskBeforeApply({ taskId: record.id });
        const { auditEmployeeName, launchEmployeeName } = data;
        setActionLoading(false);
        setModalData({
            ...record,
            auditEmployeeName,
            launchEmployeeName,
            reRateEmployeeList,
            executorName: user.employeeName
        });
        setModalType(modalTypes.RE_RATE_APPLY.key);
        setModalVisible(true);
        return null;
    };

    React.useImperativeHandle(ReScoreModalRef, () => ({
        showReScoreModal,
        showReScoreDetail: async record => {
            setActionLoading(true);
            const { code, data } = await getOperateDetails(record.id, record.operateRecordId);
            setActionLoading(false);
            if (code === 10000) {
                setModalData(data);
                setModalType(modalTypes.RE_RATE_APPLY_DETAIL.key);
                setModalVisible(true);
            }
        }
    }));

    const onOk = async (values, setConfirmLoading, _, data) => {
        const { files, employeeList, ...rest } = values;
        const paramsData = {
            ...rest,
            taskId: data.id,
            employeeList: employeeList.map(item => JSON.parse(item)),
            resourceRelation: values.files ? [values.files.uploadResourceFile] : []
        };
        setConfirmLoading(true);
        const res = await applyReScore(paramsData);
        setConfirmLoading(false);
        if (res.code === 10000) {
            setModalVisible(false);
            message.success('提交成功！');
        } else {
            message.error(res.msg || '提交失败');
        }
        return null;
    };

    return (
        <>
            <ScoreModal
                data={modalData}
                visible={modalVisible}
                type={modalType}
                setVisible={setModalVisible}
                onOk={onOk}
                isDetail={isDetail}
            ></ScoreModal>
        </>
    );
};

export default ReRateApplyModal;
